package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.chapter.ChapterVo;
import com.atguigu.eduservice.feign.OrderApi;
import com.atguigu.eduservice.frontvo.CourseFrontVo;
import com.atguigu.eduservice.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/12/16
 */
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderApi orderApi;

    //1、添加查询带分页查询
    @PostMapping("getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        Map<String, Object> map = courseService.getFrontCourseList(pageCourse, courseFrontVo);


        return R.ok().data(map);
    }


    //2、课程详情方法
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseWebVo = courseService.getFrontCourseInfo(courseId);

        List<ChapterVo> chapterVideo = chapterService.getChapterVideo(courseId);

        //根据课程id和用户id查出当前课程是否已经支付
        boolean buyCourse = orderApi.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideo",chapterVideo).data("isBuy",buyCourse);
    }


    //根据课程id查询课程信息
    @GetMapping("getCourseInfoById/{id}")
    public CourseWebVoOrder getCourseInfoById(@PathVariable String id) {
        CourseWebVo frontCourseInfo = courseService.getFrontCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(frontCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }



}
